/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package multithreadserver;
import java.net.*;
import java.io.*;
import java.util.Random;
/**
 *
 * @author Brady Itkin
 */
public class thread extends Thread {
    Protocol protohandler;
    private Socket scket;
    public thread(Socket scket) {
        super("thread");
        this.scket = scket;
    }
    public void run() {
        PrintWriter outstream = null;
        BufferedReader instream = null;
        Random gen = new Random();
        int usernum=gen.nextInt(9001);
        
        try {
            outstream = new PrintWriter(scket.getOutputStream(), true);
            instream = new BufferedReader(
                    new InputStreamReader(
                    scket.getInputStream()));
            String inputln;
            String outputln="";
            protohandler = new Protocol();
            protohandler.Writeln("NEW ACCEPT");
            outstream.println(protohandler.ProcessInput(null));
            while ((inputln = instream.readLine()) != null) {
                if (protohandler.state==Protocol.SAYSOMETHING) {
                    inputln+="---"+usernum;
                }
                outputln = protohandler.ProcessInput(inputln);
                outstream.println(outputln);
                if (inputln.contains("Bye!")) {
                    break;
                }
            }
            instream.close();
            outstream.close();
            scket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}
